package com.kotiln.lgc.lib_voice.asr

import android.content.Context
import android.util.Log
import com.baidu.speech.EventListener
import com.baidu.speech.EventManager
import com.baidu.speech.EventManagerFactory
import com.baidu.speech.asr.SpeechConstant
import org.json.JSONObject

/**
 *@fileName VoiceAsr
 *@date2021/1/21  10:44
 *@author VoiceAsr$
 *@describe TODO
 *@desc 语音识别
 */
object VoiceAsr {

    //语音识别管理器对象
    private lateinit var asr: EventManager

    private lateinit var asrJson:String

    //初始化Asr
    fun initAsr(mContext: Context, listener: EventListener) {

        val map=HashMap<Any,Any>()
        map[SpeechConstant.ACCEPT_AUDIO_VOLUME]=true
        map[SpeechConstant.ACCEPT_AUDIO_DATA]=false
        map[SpeechConstant.DISABLE_PUNCTUATION]=false
        map[SpeechConstant.PID]=15373 //15373
        asrJson=JSONObject(map as Map<Any,Any>).toString()

        asr = EventManagerFactory.create(mContext, "asr")
        asr.registerListener(listener)
    }

    //开始识别
    fun startAsr(){
        asr.send(SpeechConstant.ASR_START, asrJson,null,0,0)
    }

    //停止识别
    fun stopAsr(){
        asr.send(SpeechConstant.ASR_STOP, null,null,0,0)
    }

    //取消识别
    fun cancelAsr(){
        asr.send(SpeechConstant.ASR_CANCEL,null,null,0,0)
    }

    //销毁识别
    fun releaseAsr(listener: EventListener){
        asr.unregisterListener(listener)
    }
}